﻿#include "CheckPosition.h"
#include "ui_CheckPosition.h"

CheckPositionDialog::CheckPositionDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CheckPosition)
{
    ui->setupUi(this);

}

CheckPositionDialog::~CheckPositionDialog()
{
    delete ui;
}

void CheckPositionDialog::setImage(const QImage &img)
{
    ui->radioButton_top->setChecked(false);
    ui->radioButton_bottom->setChecked(false);
    ui->radioButton_left->setChecked(false);
    ui->radioButton_right->setChecked(false);

    m_image = img;
    ui->label->setPixmap(QPixmap::fromImage(img).scaled(ui->label->size()));//setPixelmap(QPixmap::fromImage(img));
}

void CheckPositionDialog::on_radioButton_top_clicked(bool checked)
{
    Q_UNUSED(checked);
    m_measurementTypt = TOP;
    drawLine();
}

void CheckPositionDialog::on_radioButton_bottom_clicked(bool checked)
{
    Q_UNUSED(checked);
    m_measurementTypt = BOTTOM;
    drawLine();
}

void CheckPositionDialog::on_radioButton_left_clicked(bool checked)
{
    Q_UNUSED(checked);
    m_measurementTypt = LEFT;
    drawLine();
}

void CheckPositionDialog::on_radioButton_right_clicked(bool checked)
{
    Q_UNUSED(checked);
    m_measurementTypt = RIGHT;
    drawLine();
}

void CheckPositionDialog::drawLine()
{
    cv::Mat m_cvimage = QImage2cvMat(m_image).clone();
    cv::Mat grayImage;//临时变量和目标图的定义
    cv::cvtColor(m_cvimage, grayImage, cv::COLOR_BGR2GRAY);

    double* pixSum;
    int dataLength;
    if(m_measurementTypt == LEFT || m_measurementTypt == RIGHT)
    {
        dataLength = grayImage.cols;
    }
    else
    {
        dataLength = grayImage.rows;
    }
    qDebug() << "dataLength = " << dataLength;
    if(dataLength == 0) return;
    //QVector<double> pixSum(dataLength);
    pixSum = new double[dataLength];
    for(int k = 0; k < dataLength; k++)
    {
        pixSum[k] = 0;
    }

    for (int j = 0; j < grayImage.rows; j++)
    {
        const uchar* current = grayImage.ptr<const uchar>(j); // current row
        for (int i = 0; i < grayImage.cols; i++)
        {
            if(m_measurementTypt == LEFT || m_measurementTypt == RIGHT)
            {
                pixSum[i] = pixSum[i] + current[i];// 每一列的值求和
            }
            else
            {
                pixSum[j] = pixSum[j] + current[i];// 每一行的值求和
            }
        }
    }

    double m_max = gsl_stats_max(pixSum, 1, dataLength);
    double m_min = gsl_stats_min(pixSum, 1, dataLength);
    double t = m_min + 0.1 * (m_max - m_min);
    qDebug() << "max = " << m_max;
    qDebug() << "min = " << m_min;
    qDebug() << "t = " << t;
    int ret = 0;
    if(m_measurementTypt == LEFT || m_measurementTypt == TOP)
    {
        for(int k = 0; k < dataLength - 1; k++)
        {
            if(pixSum[k] <= t && t <= pixSum[k + 1])
            {
                //qDebug() << "pixSum[k] = " << pixSum[k] << "k = " << k;
                ret = k;
                k = dataLength;
            }
        }
    }
    else
    {
        for(int k = dataLength - 1; k > 0; k--)
        {
            if(pixSum[k] <= t && t <= pixSum[k - 1])
            {
                //qDebug() << "pixSum[k] = " << pixSum[k] << "k = " << k;
                ret = k;
                k = 0;
            }
        }
    }

    if(m_measurementTypt == LEFT || m_measurementTypt == RIGHT)
    {
        cv::Mat H(101, dataLength, CV_8UC3, cv::Scalar(0, 0, 0));
        for(int i = 0; i < dataLength; i++)
        {
            cv::line(H, cv::Point(i, 0), cv::Point(i, pixSum[i] * 100 / m_max), cv::Scalar(200, 200, 200), 1, cv::LINE_AA);
        }
        cv::line(H, cv::Point(ret, 0), cv::Point(ret, 100), cv::Scalar(255, 255, 255), 1, cv::LINE_AA);
        //cv::imshow("H",H);
        ui->label_H->setPixmap(QPixmap::fromImage(cvMat2QImage(H).scaled(ui->label_H->size())));
    }
    else
    {
        cv::Mat V(dataLength, 101, CV_8UC3, cv::Scalar(0,0,0));
        for(int i = 0; i < dataLength; i++)
        {
            cv::line(V, cv::Point(0, i), cv::Point(pixSum[i] * 100 / m_max, i), cv::Scalar(200,200,200), 1, cv::LINE_AA);
        }
        cv::line(V, cv::Point(0, ret), cv::Point(100, ret), cv::Scalar(255,255,255), 1, cv::LINE_AA);
        //cv::imshow("V",V);
        ui->label_V->setPixmap(QPixmap::fromImage(cvMat2QImage(V).scaled(ui->label_V->size())));
    }

    if(m_measurementTypt == LEFT || m_measurementTypt == RIGHT)
    {
        cv::line( m_cvimage, cv::Point(ret, 0), cv::Point(ret, m_cvimage.rows), cv::Scalar(0,255,0), 3, cv::LINE_AA);
    }
    else
    {
        cv::line( m_cvimage, cv::Point(0, ret), cv::Point(m_cvimage.cols, ret), cv::Scalar(0,255,0), 3, cv::LINE_AA);
    }
    delete[] pixSum;

    ui->label->setPixmap(QPixmap::fromImage(cvMat2QImage(m_cvimage).scaled(ui->label->size())));
}
